' ****************************************************************
' This is free software licensed under the NUnit license. You
' may obtain a copy of the license as well as information regarding
' copyright ownership at http://nunit.org/?p=license&r=2.4.
' ****************************************************************

Option Explicit On 

Imports System
Imports NUnit.Framework
Imports System.Diagnostics
Imports log4net
Imports NHibernate
Imports NHibernate.Cfg
Imports System.Collections

Namespace NCS

    <TestFixture()> _
    Public Class AllTest

        Private configuration As Configuration
        Private factory As ISessionFactory
        Private session As ISession

        <SetUp()> _
        Protected Sub SetUp()
            configuration = Class1.configuration
            factory = configuration.BuildSessionFactory()
            session = factory.OpenSession()
        End Sub


        <Test()> Public Sub testcalcField()
            Dim nvlv As Integer
            Dim o As Object
            o = session.CreateQuery("select max(dc.id) from doChanel dc").UniqueResult()
            If Not o Is Nothing Then
                nvlv = CInt(o) + 1
            Else
                nvlv = 0
            End If
        End Sub
        <Test()> Public Sub testuser1()
            Dim o As IList
            o = session.CreateCriteria(GetType(muser)).List()
        End Sub
        <Test()> Public Sub testStandard()
            Dim o As IList
            o = session.CreateCriteria(GetType(standard)).List()
        End Sub
        <Test()> Public Sub testvoltnew()

            'Dim configuration As Configuration = Class1.configuration
            'Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            'Dim session As ISession = factory.OpenSession()
            ''Dim o As IList
            ''Dim v As VoltNew
            ''v = TryCast(session.Get(GetType(VoltNew), 18), VoltNew)
            'Dim mysig As Sig
            'mysig.Initialize()
            'loadsignew(session, mysig, 18)
            'savesignew(session, mysig, -1)
            'loadsignew(session, mysig, -1)
        End Sub

        'usehistory
        'CurveHis
        'diChanel
        <Test()> Public Sub newnetset()
            Dim newmethod1 As New netSet
            newmethod1.ip = "127.0.0.1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub

        <Test()> Public Sub newdochanel()
            Dim newmethod1 As New doChanel
            newmethod1.cname = "sample1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub newdichanel()
            Dim newmethod1 As New diChanel
            newmethod1.cname = "sample1"
            newmethod1.id = 18
            session.Save(newmethod1)
            session.Flush()
        End Sub

        <Test()> Public Sub newweight()
            Dim newmethod1 As New weight
            newmethod1.name = "sample1"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub newcurvehistory()
            'Dim configuration As Configuration = Class1.configuration
            'Dim factory As ISessionFactory = configuration.BuildSessionFactory()
            'Dim session As ISession = factory.OpenSession()
            'Dim newmethod1 As New CurveHis
            'newmethod1.ele = "C"
            'session.Save(newmethod1)
            'session.Flush()
        End Sub

        <Test()> Public Sub newusehistory()
            Dim newmethod1 As New usehistory
            newmethod1.endtime = Now()
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub newSample()
            Dim newmethod1 As New SampleAve
            newmethod1.name = "a"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub newChanel()
            Dim newmethod1 As New chanel
            newmethod1.ele = "a"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub newmethod()
            Dim newmethod1 As New method
            newmethod1.cname = "all casade"
            newmethod1.cuser = "general"
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub testCurveData()
            Dim cdata As NCS.curveData
            'm = DirectCast(session.CreateSQLQuery("select * from curvedata").AddEntity(GetType(NCS.curveData)).List()(0), NCS.curveData)
            'm.jieqi = False
            'm.flushtime = 6
            'm.curveXishuList(0).x0 = 0.9
            cdata = New curveData
            Dim cxishu As curveXishu
            cxishu = New NCS.curveXishu
            cxishu.x0 = 0
            cxishu.x1 = 0
            cxishu.ele = "C"
            cxishu.cishu = 1
            Dim newmethod1 As New method
            newmethod1.cname = "all casade"
            newmethod1.cuser = "general"




            cxishu.method = newmethod1
            newmethod1.curveXishuList.Add(cxishu)
            'session.Save(cxishu)
            'session.Flush()
            cdata.curveXishu = cxishu
            cxishu.curveDataList.Add(cdata)
            'session.Save(cdata)
            session.Save(newmethod1)
            session.Flush()
        End Sub
        <Test()> Public Sub childAdd()
            Dim m As NCS.method
            Dim l As IList
            l = session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()
            If l.Count > 0 Then
                m = DirectCast(l(0), NCS.method)
                Dim x1 As New NCS.curveXishu()
                x1.ele = "C"
                x1.method = m
                m.curveXishuList.Add(x1)
                session.Save(x1)
                session.Flush()
            Else

            End If
        End Sub


        <Test()> Public Sub childDel()
            Dim m As NCS.method
            Dim l As IList
            l = session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()
            If l.Count > 0 Then
                m = DirectCast(l(0), NCS.method)
                Dim x1 As NCS.curveXishu
                x1 = m.curveXishuList(0)
                m.curveXishuList.Remove(x1)
                session.Delete(x1)
                session.Flush()
            End If
        End Sub

        <Test()> Public Sub testMethod()
            Dim m As NCS.method
            m = DirectCast(session.CreateSQLQuery("select * from method where cuser='cs' and cname='gen'").AddEntity(GetType(NCS.method)).List()(0), NCS.method)
            m.jieqi = False
            m.flushtime = 6
            m.curveXishuList(0).x0 = 0.9
            session.Flush()
        End Sub
        <Test()> Public Sub testUser()

            log4net.Config.XmlConfigurator.Configure()

            Dim configuration As New Configuration()

            Console.WriteLine("Loading hibernate.cfg.xml...")
            configuration.Configure()
            Console.WriteLine("Loading mapping files in this executable...")
            configuration.AddAssembly(System.Reflection.Assembly.GetExecutingAssembly())
            Dim factory As ISessionFactory = configuration.BuildSessionFactory()

            Dim firstId As String

            Using session As ISession = factory.OpenSession()

                Console.WriteLine("Saving a test muser object to the database...")

                Dim user As New muser()
                user.mname = "hello3"
                'user.EmailAddress = TestEmailAddress;
                'user.Password = TestPassword;
                'user.UserName = TestUserName;

                ' Save the user object, then flush the session to
                ' commit it to the database.  Upon return, the
                ' user object should have a new ID.

                session.Save(user)
                session.Flush()

                firstId = user.mname

                Console.WriteLine("id" & user.ID.ToString())

                'Debug.Assert(
                '    firstId > 0, "The Id should have been returned.");

                ' If you get a failure here (e.g. because user.Id is zero),
                ' make sure the hibernate.connection.release_mode setting
                ' in hibernate.cfg.xml is set to "on_close".
                '
                ' Explanation:  The Id of the underlying Users table is an
                ' AutoNumber. This means Access/Jet assigns the value of
                ' the ID when the record is first created.  The Jet driver
                ' for NHibernate retrieves the new ID by sending a special
                ' SQL statement ("select @@identity").  The @@Identity 
                ' variable contains the last ID generated by Jet.  
                '
                ' However, NHibernate is aggressive in closing the
                ' ADO.NET database connection between SQL statements.  This
                ' is the correct design (normally) but has the unfortunate
                ' side-effect of invalidating the @@Identity variable.
                ' NHibernate closes the connection between the INSERT
                ' statment (when saving) and the SELECT @@Identity statement.
                '
                ' The temporary work around is to enable the older behavior
                ' of forcing NHibernate to keep the underlying connection open
                ' throughout the lifetime of the session.  This will likely
                ' be fixed in a future version of the library.
                Console.WriteLine("The muser object was assigned an ID of " & firstId & "!")
            End Using


            ' ---------------------
            ' Test 2: Load the User
            ' ---------------------
            '
            ' Try reloading the user back from the database.  Compare
            ' the fields with the properties originally saved.  This is
            ' performed with a completely different session and User object
            ' to show that it was truly loaded from the database.

            Using session As ISession = factory.OpenSession()

                Console.WriteLine("Attempting to reload User with ID " & firstId & "...")

                Dim user As muser = session.Load(Of muser)(firstId)
                Console.WriteLine("id" & user.ID.ToString())
                Debug.Assert(user.mname = firstId, "The wrong Id field was returned.")
                user = session.Load(Of muser)(firstId)
                Console.WriteLine("id" & user.ID.ToString())
                'Debug.Assert(
                '    user.EmailAddress == TestEmailAddress,
                '    "The wrong EmailAddress was returned.");

                'Debug.Assert(
                '    user.Password == TestPassword,
                '    "The wrong Password was returned.");

                'Debug.Assert(
                '    user.UserName == TestUserName,
                '    "The wrong UserName was returned.");


                Console.WriteLine("The user was reloaded successfully!")
            End Using


            ' -----------------------
            ' Test 3: Delete the muser
            ' -----------------------

            Using session As ISession = factory.OpenSession()

                ' Load the user again (to get an object reference),
                ' then delete it from the database.

                Console.WriteLine("Deleting the user...")
                Dim user As muser = session.Load(Of muser)(firstId)
                session.Delete(user)

                ' Now try to reload the user.  This should fail.

                Try
                    Console.WriteLine("Confirming the user record is deleted...")
                    Dim deletedUser As muser = session.[Get](Of muser)(firstId)
                    Throw New InvalidOperationException("The load should not have succeeded.")
                Catch ex As ObjectDeletedException
                    ' eat
                    Console.WriteLine(ex.Message)
                End Try

                session.Flush()
            End Using


            ' I hope this sample was useful.  Free free to contact
            ' me at dave@fairmetric.com if you have questions or feedback.

            Console.WriteLine("Done.  Press Enter to exit...")
            Console.ReadLine()

        End Sub

    End Class

End Namespace
